package cn.ecpay.unionpay.settle.Common;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间格式化工具类
 * Created by zhaoyanzeng on 2018/11/8.
 */
public class DateUtil {
    /**
     * yyyyMMdd
     *
     * @param date
     * @return YYYYMMMDD
     */
    public static String getDateTo8(Date date) {
        if (date == null) {
            date = new Date();
        }
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        return sf.format(date);
    }

    /**
     * yyyyMMddHHmmss
     *
     * @param date
     * @return yyyyMMddHHmmss
     */
    public static String getDateTo14(Date date) {
        if (date == null) {
            date = new Date();
        }
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sf.format(date);
    }

    /**
     * yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getDateTo19(Date date) {
        if (date == null) {
            date = new Date();
        }
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(date);
    }


    /**
     * 6 位日期格式
     *
     * @param date
     * @return YYYYMMMDD
     */
    public static String getDateTo6(Date date) {
        if (date == null) {
            date = new Date();
        }
        SimpleDateFormat sf = new SimpleDateFormat("yyMMdd");
        return sf.format(date);
    }

    /**
     * 功能描述：返回小时
     *
     * @param date 日期
     * @return 返回小时
     */
    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);

    }

    /**
     * 功能描述：返回分
     *
     * @param date
     *            日期
     * @return 返回分钟
     */
    public static int getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

}
